package com.hzh.cusviewdemo.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.hzh.cusviewdemo.Adapter.LeftListAdapter;
import com.hzh.cusviewdemo.Adapter.RightListAdapter;
import com.hzh.cusviewdemo.Base.ApiResponse;
import com.hzh.cusviewdemo.Base.ApiService;
import com.hzh.cusviewdemo.Bean.TypeData;
import com.hzh.cusviewdemo.Custom.TopSmoothScroller;
import com.hzh.cusviewdemo.R;
import com.hzh.cusviewdemo.Util.RetrofitUtils;

import java.util.List;

import dalvik.system.DexClassLoader;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChooseClassActivity extends AppCompatActivity {

    private RecyclerView acc_left_list;
    private RecyclerView acc_right_list;

    private LeftListAdapter leftListAdapter; // 左边列表适配器
    private RightListAdapter rightListAdapter; // 右边列表适配器

    private LinearLayoutManager rightManager; // 右侧数据布局管理器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_class);

        initView();
        new Thread(this::initData).start();
    }

    private void initData() {
        // 获取对应数据并加载出来
        ApiService apiService = RetrofitUtils.getInstance().getRetrofit().create(ApiService.class);
        apiService.getCategory().enqueue(new Callback<ApiResponse>() {
            @Override
            public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                if (response.body() == null)
                    return;
                if (response.body().getCode().equals("SUCCESS")) {
                    // 有数据，将Data解析好
                    String jsonData = JSON.toJSONString(response.body().getData());
                    List<TypeData> typeData = JSON.parseArray(jsonData, TypeData.class);
                    runOnUiThread(() -> {
                        if (typeData.size() > 0) {
                            // 设置左侧的数据
                            leftListAdapter = new LeftListAdapter(typeData);
                            leftListAdapter.setOnTypeListener(position -> {
                                // 点击具体的类别之后, 将右侧数据
                                LinearSmoothScroller smoothScroller = new TopSmoothScroller(ChooseClassActivity.this);
                                smoothScroller.setTargetPosition(position);
                                rightManager.startSmoothScroll(smoothScroller);  // 平滑的滚动到对应位置
//                                    moveRightToPosition(position);   // 瞬间到对应位置
                            });
                            acc_left_list.setLayoutManager(new LinearLayoutManager(ChooseClassActivity.this));
                            acc_left_list.setAdapter(leftListAdapter);

                            // 设置右侧的数据
                            rightListAdapter = new RightListAdapter(typeData, ChooseClassActivity.this);
                            rightManager = new LinearLayoutManager(ChooseClassActivity.this);
                            acc_right_list.setLayoutManager(rightManager);
                            acc_right_list.setAdapter(rightListAdapter);
                        } else {
                            ToastUtils.showShort("无数据！");
                        }
                    });
                } else {
                    // 无
                    ToastUtils.showShort("数据加载失败！");
                }
            }

            @Override
            public void onFailure(Call<ApiResponse> call, Throwable t) {
                ToastUtils.showShort("数据加载失败！");
            }
        });
    }

    /**
     * 将右边列表滑动
     *
     * @param position
     */
    private void moveRightToPosition(int position) {
        //获取当前recycleView屏幕可见的第一项和最后一项的Position
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) acc_right_list.getLayoutManager();
        int firstItem = linearLayoutManager.findFirstVisibleItemPosition();
        int lastItem = linearLayoutManager.findLastVisibleItemPosition();
        //然后区分情况
        if (position <= firstItem) {
            //当要置顶的项在当前显示的第一个项的前面时
            acc_right_list.scrollToPosition(position);
        } else if (position <= lastItem) {
            //当要置顶的项已经在屏幕上显示时，计算它离屏幕原点的距离
            int top = acc_right_list.getChildAt(position - firstItem).getTop() - 40 * 2;
            acc_right_list.scrollBy(0, top);
        } else {
            //当要置顶的项在当前显示的最后一项的后面时
            acc_right_list.scrollToPosition(position);
            //记录当前需要在RecyclerView滚动监听里面继续第二次滚动
        }
    }


    private void initView() {
        acc_left_list = findViewById(R.id.acc_left_list);
        acc_right_list = findViewById(R.id.acc_right_list);
    }
}